﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Passwords.Controls
{
    public class TilePanel : Panel
    {
        const int MAX_TILES = 5;
        static TilePanel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TilePanel), new FrameworkPropertyMetadata(typeof(TilePanel)));
        }



        public double TileWidth
        {
            get { return (double)GetValue(TileWidthProperty); }
            set { SetValue(TileWidthProperty, value); }
        }

        public static readonly DependencyProperty TileWidthProperty =
            DependencyProperty.Register("TileWidth", typeof(double), typeof(TilePanel), new UIPropertyMetadata(200d));




        public double TileHeight
        {
            get { return (double)GetValue(TileHeightProperty); }
            set { SetValue(TileHeightProperty, value); }
        }

        public static readonly DependencyProperty TileHeightProperty =
            DependencyProperty.Register("TileHeight", typeof(double), typeof(TilePanel), new UIPropertyMetadata(26d));


        protected override Size MeasureOverride(Size availableSize)
        {
            int n = Children.Count;

            int maxRows = (int)(availableSize.Height / TileHeight);            
            int columns = Math.Max(1, (int)(availableSize.Width / TileWidth));
            if (columns > MAX_TILES) columns = MAX_TILES;
//            int columns = maxRows <= n ? 1 : Math.Max(1, (int)(availableSize.Width / TileWidth));

            int rows = (int)((n + columns - 1) / columns);
            double height = TileHeight * rows;
            double w = availableSize.Width;
            if (double.IsInfinity(w)) w = 0d;
            double width = Math.Max(TileWidth * columns, w);

            return new Size(width, height);
        }

        protected override Size ArrangeOverride(Size finalSize)
        {
            int n = Children.Count;

            int maxRows = (int)(finalSize.Height / TileHeight);
//            int columns = Math.Max(1, (int)(finalSize.Width / TileWidth));
            int columns = maxRows > n ? 1 : Math.Max(1, (int)(finalSize.Width / TileWidth));
            if (columns > MAX_TILES) columns = MAX_TILES;
            int rows = (int)((n + columns - 1) / columns);

            int column = 0;
            //double cw = Math.Max(TileWidth, finalSize.Width / columns); ;
            double cw = TileWidth;
            double ch = TileHeight;
            double top = 0;
            double maxHeight=finalSize.Height;
            if (columns == 1) cw = finalSize.Width;
            foreach (UIElement child in Children)
            {
                Rect childRect = new Rect(column * cw, top, cw, ch);
                child.Arrange(childRect);

                top += ch;
                if ((top + ch) > maxHeight)
                {
                    column++;
                    top = 0;
                }
            }

            double height = TileHeight * rows;
            double width = Math.Max(TileWidth * columns, finalSize.Width);
            return new Size(width, height);
        }
    }
}
